<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-05-18 13:45:09
 * @Description: 用户的退款记录
 */
class AcountUseRefund extends Eloquent
{
    protected $table = "acount_user_refund_record";

    public static $return_pay_type=array(
        "1"=>"线下支付现金",
        "7"=>"刷卡",
        '11'=>'线下支票',
    );
    
    /**
     * @Author:      lyt
     * @DateTime:  2017-05-18 14:06:32
     * @Description: 退款具体方法
     *金额单位分
     */
    public static function return_amount($user_id,$pay_type,$shop_id,$amount,$create_type,$create_by,$au_id,$desc){
        try{
            $response = DB::transaction(function() use ($user_id,$pay_type,$shop_id,$amount,$create_type,$create_by,$au_id,$desc){
                //交易流水
                // $tran_no = date('Ymd',time()).Common::generateCode();
                $tran_no = date('YmdHis',time()).Common::generateCode(8);
                $response= array('code' => 0, 'msg' => 'ok', 'result' => array());

                //记录用户退款记录
                $auu=new AcountUseRefund();
                $auu->user_id=$user_id;
                $auu->pay_type=$pay_type;
                $auu->amount=$amount;
                $auu->status=1;
                $auu->create_type=$create_type;
                $auu->create_by=$create_by;
                $auu->tran_no=$tran_no;
                $auu->shop_id=$shop_id;
                $auu->implement_time=Common::currentTimeString();
                $auu->au_id=$au_id;
                $auu->desc=$desc;
                $auu->save();
                $arr=AcountUserRecharge::getUseRecharge($shop_id,$user_id,$amount,1,"desc");
                if(sizeof($arr)>0){
                  foreach ($arr as $_arr) {
                    //记录了用户的充值消费记录
                    $aurs=new AcountUserRechargeSpend;
                    $aurs->aurr_id=$_arr->id;
                    $aurs->user_id=$user_id;
                    $aurs->buss_id=$auu->id;
                    $aurs->type=4;//记录退款消费
                    $aurs->amount=$_arr->user_amount;
                    $aurs->status=1;
                    $aurs->tran_no=$tran_no;
                    $aurs->shop_id=$shop_id;
                    $aurs->implement_time=date("Y-m-d H:i:s",time());
                    $aurs->save();
                    //更改充值记录消费的记录
                    AcountUserRecharge::where('id',$_arr->id)->update(array('current_amount'=> $_arr->current_amount -$_arr->user_amount,));
                    //记录个人 各种账户余额
                    Account::record($user_id,$_arr->shop_id,$_arr->acount_type,$tran_no,2,208,$_arr->user_amount,'用户退款支出,退款记录:'.$auu->id);
                  }
                }else{
                  DB::rollback();
                  return Lang::get("scs_error.amount_cant_return_error_40105");
                }
                return array('code' => 0, 'msg' => 'ok', 'result' => array("auref_id"=>$auu->id));
            });
            return $response;
        } catch (Exception $e) {
            Log::error("error", array("system" => "Account" ,"event" => "return_amount" ,"exception" => $e));
            return false;
        }
    }

}